﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapManager : MonoBehaviour
{

    public int[,] mapData = new int[10, 10]{
        {0,1,0,0,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0,1,0},
        {0,1,0,0,0,0,0,1,0,0},
        {0,0,1,0,0,0,1,0,0,0},
        {0,0,0,1,0,1,0,0,0,0},
        {0,0,0,0,1,0,0,0,0,0},
        {0,0,0,0,0,1,0,0,0,0},
        {0,0,0,0,0,0,1,0,0,0},
        {0,0,0,0,0,0,0,1,0,0},
        {0,0,0,0,0,0,0,0,0,0},
    };

    public GameObject playerCube;

    public int PlayerIndex = 0;

    public int EndIndex = 0;

    public GameObject endPositionCube;

    public Vector3 startPosition = new Vector3(0, 1, 0);

    public Vector3 endPosition = new Vector3(9, 1, 9);

    public List<GameObject> cubeList = new List<GameObject>();

    public GameObject cubeTemp;

    Color pathColor = Color.gray;

    List<Point> MapPointInfo = new List<Point>();

    // Use this for initialization
    void Start()
    {

        playerCube.transform.position = startPosition;

        endPositionCube.transform.position = endPosition;

        for (int i = 0; i < 10; i++)
        {

            for (int j = 0; j < 10; j++)
            {

                GameObject temp = GameObject.Instantiate(cubeTemp);

                temp.transform.position = new Vector3(j, 0, i);

                if (mapData[i, j] == 1)
                {

                    temp.GetComponent<Renderer>().material.color = Color.red;

                }

                temp.name = (i * 10 + j).ToString();

                cubeList.Add(temp);

            }

        }

        cubeTemp.SetActive(false);

        Point[] points = AStartCom.getInstance().GetResultPath(mapData, startPosition, endPosition, cubeList);

        for (int i = 0; i < points.Length; i++)
        {

            cubeList[points[i].x * 10 + points[i].z].GetComponent<Renderer>().material.color = Color.green;

        }

    }

}


public class Point
{

    public int x, z;

    public float F, G, H;

    public Point ParentPoint;

    Vector3 endPoint;

    public bool IsWall = false;

    public bool IsEquals(Point po)
    {

        if (x == po.x && z == po.z)
        {

            return true;

        }

        return false;

    }

    public Point()
    {


    }

    public Point(int x, int z, Point parent, Vector3 endPoint)
    {

        this.x = x;

        this.z = z;

        //进行估价
        ParentPoint = parent;

        this.endPoint = endPoint;

        CalculateG();

        CalculateF();

        H = F + G;

    }

    public void UpdateParent(Point parent, float g)
    {

        ParentPoint = parent;

        G = g;

        F = G + H;

    }

    public static float CalculateGValue(Point p, Point parent)
    {

        float parentG = 0;

        float currentG = 0;

        if (parent != null)
        {

            parentG = parent.G;

        }

        currentG = Vector2.Distance(new Vector2(p.x, p.z), new Vector2(parent.x, parent.z)) + parentG;

        return currentG;

    }

    /// <summary>
    /// 计算G点
    /// </summary>
    /// <returns></returns>
    void CalculateG()
    {

        float parentG = 0;

        Vector2 parentPoint = Vector2.zero;

        if (ParentPoint != null)
        {

            parentG = ParentPoint.G;

            parentPoint = new Vector2(ParentPoint.x, ParentPoint.z);

        }

        G = Vector2.Distance(new Vector2(x, z), parentPoint) + parentG;

    }

    /// <summary>
    /// 计算F点
    /// </summary>
    /// <returns></returns>
    public void CalculateF()
    {

        F = Mathf.Abs(endPoint.x - x) + Mathf.Abs(endPoint.z - z);

    }

    public static void CalculateF(Point now, Point end)
    {

        float h = Mathf.Abs(end.x - now.x) + Mathf.Abs(end.z - now.z);

        float g = 0;

        if (now.ParentPoint == null)
        {

            g = 0;

        }
        else
        {

            g = Vector2.Distance(new Vector2(now.x, now.z), new Vector2(now.ParentPoint.x, now.ParentPoint.z)) + now.ParentPoint.G;

        }

        float f = g + h;

        now.F = f;

        now.G = g;

        now.H = h;

    }

    //     public void Init(Vector3 pos, Point parent = null)
    //     {
    // 
    //         Position = pos;
    // 
    //         ParentPoint = parent;
    // 
    //     }

    public void UpDateParent(Point parent, int g)
    {

        ParentPoint = parent;

        G = g;

        F = G + H;

    }

}

